<?php
    class Home extends CActiveRecord
    {
        public function getTips()
        {
            $sql = "SELECT title, url, color, icon FROM c_tips WHERE type=0 ORDER BY position DESC LIMIT 5";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();        
            return $rows;      
        }

        public function getGeneral()
        {
            $sql = "SELECT title, url, color, icon FROM c_tips WHERE type=1 ORDER BY position DESC LIMIT 5";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();        
            return $rows;   
        }

        public function getPattern()
        {
            $sql = "SELECT keyword FROM c_blacklist_chat";
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();
            $blacklist = array();            
            foreach($rows as $i=>$row){
                $blacklist["pattern"][$i] = "/" . $row["keyword"] . "/si";
                $blacklist["replace"][$i] = "******";
            }
            return $blacklist;
        }

        public function getNewsTipsByArrIcon($limit){
            $str_sql = "";
            $str_sql .=" AND button >0"; 
            $sql = "SELECT title, url, color, button FROM c_tips WHERE 1 ".$str_sql." ORDER BY position DESC LIMIT ".$limit;
            $command = Yii::app()->db->createCommand($sql);
            $rows = $command->queryAll();        
            return $rows; 
        }

        public function getDataForum(){
            return array();
            /*$cacheService = new CacheService("Home","getDataForum");
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            if ($cache == false) 
            {
                $row = json_decode(@file_get_contents("http://diendan.mtop.vn/Thread.php"), true); 
                Yii::app()->cache->set($key, $row, ConstantsUtil::TIME_CACHE_3600);
            }
            else
            {
                $row=$cache;
            }
            return $row;*/
        }
        

        public function getDataByType($table,$page,$num_per_page,$isOnline=2){
            $begin = ($page - 1) * $num_per_page;
            $cacheService = new CacheService("Home","getDataShowHomeByCat",$table,$page.$num_per_page);
            $key = $cacheService->createKey();
            $cache = Yii::app()->cache->get($key);
            $cache=false; 
            if ($cache == false) 
            {
                $str_sql = "";
                $str_order = " ORDER BY download DESC";
                $connect =Yii::app()->db;
                if($table=="c_game"){     
                    if($isOnline <2){
                        $str_sql .= " AND isOnline = ".$isOnline;
                    } 
                    $field = "id,title,alias,introtext,picture,download,hit,create_date,isHot,percent_wapmaster,isOnline,isSms";
                }elseif($table=="c_news"){
                    $field = "id,title,alias,introtext,picture,hit,create_date,isHot";
                    $str_order = " ORDER BY hit DESC";
                }elseif($table=="c_tuvi"){
                    $field = "id,title,alias,introtext,picture,create_date";
                    $str_order = " ORDER BY id DESC";
                }else{
                    $field = "id,title,alias,introtext,picture,download,hit,create_date,isHot";
                }

                $sql="SELECT $field FROM ".$table."   
                WHERE status=1 ".$str_sql." ".$str_order." LIMIT ".$begin.",".$num_per_page."";
                $command=$connect->createCommand($sql);
                $row = $command->queryAll();
                switch($table){
                    case "c_game":
                        $type = 1;
                        break;
                    case "c_video":
                        $type = 2;
                        break;
                    case "c_album":
                        $type = 3;
                        break;
                    case "c_news":
                        $type = 4;
                        break;
                    case "c_tuvi":
                        $type = 5;
                        break;
                }
                for($i=0;$i<count($row);$i++){
                    $row[$i]["type"] = $type;
                }
                Yii::app()->cache->set($key, $row, ConstantsUtil::TIME_CACHE_86400);
            }
            else
            {
                $row=$cache;
            }
            return $row;
        }
        public function insertFeedBack($userId,$username,$content,$create_date){
            $sql="INSERT INTO c_feedback SET userId=:userId,username=:username,content=:content,create_date=:create_date" ;
            $command=Yii::app()->db->createCommand($sql);
            $command->bindValue(":userId", $userId, PDO::PARAM_INT);
            $command->bindValue(":username", $username, PDO::PARAM_STR);
            $command->bindValue(":content", $content, PDO::PARAM_STR);
            $command->bindValue(":create_date", $create_date, PDO::PARAM_INT);
            $insert=$command->execute();
            $insert=Yii::app()->db->getLastInsertID();
            return $insert;
        }
    }  
?>
